package cn.stylefeng.guns.sys.modular.projectPublicity.service.impl;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.plotInfo.mapper.XqInfoMapper;
import cn.stylefeng.guns.sys.modular.plotInfo.model.result.XqInfoResult;
import cn.stylefeng.guns.sys.modular.projectPublicity.entity.MainInfo;
import cn.stylefeng.guns.sys.modular.projectPublicity.mapper.MainInfoMapper;
import cn.stylefeng.guns.sys.modular.projectPublicity.model.params.MainInfoParam;
import cn.stylefeng.guns.sys.modular.projectPublicity.model.result.MainInfoResult;
import cn.stylefeng.guns.sys.modular.projectPublicity.service.MainInfoService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.List;

/**
 * <p>
 * 公示表 服务实现类
 * </p>
 *
 * @author WangZhen
 * @since 2021-01-28
 */
@Service
public class MainInfoServiceImpl extends ServiceImpl<MainInfoMapper, MainInfo> implements MainInfoService {

    @Autowired
    private XqInfoMapper xqInfoMapper;

    @Override
    public void add(MainInfoParam param) {
        MainInfo entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(MainInfoParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(MainInfoParam param) {
        MainInfo oldEntity = getOldEntity(param);
        MainInfo newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public MainInfoResult findBySpec(MainInfoParam param) {
        return null;
    }

    @Override
    public List<MainInfoResult> findListBySpec(MainInfoParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(MainInfoParam param) {
        List<XqInfoResult> list = this.xqInfoMapper.customListByUser(LoginContextHolder.getContext().getUserId());
        String[] xqIdArr = new String[list.size()];
        if (list.size() == 1) {
            xqIdArr[0] = String.valueOf(list.get(0).getId());
        } else if (list.size() >= 2) {
            for (int i = 0; i < list.size(); i++) {
                xqIdArr[i] = String.valueOf(list.get(i).getId());
            }
        }
        param.setXqIdArr(xqIdArr);
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(MainInfoParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private MainInfo getOldEntity(MainInfoParam param) {
        return this.getById(getKey(param));
    }

    private MainInfo getEntity(MainInfoParam param) {
        MainInfo entity = new MainInfo();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
